//
//  MNMViewController.m
//  MNM
//
//  Created by Taylor Clifton on 2/5/10.
//  Copyright __MyCompanyName__ 2010. All rights reserved.
//

#import "MNMViewController.h"
#import "CommunicationDelegate.h"

@implementation MNMViewController

@synthesize txtName, txtPass, lblEnterName, scrlvwMenu, contentView, bgImage, request, requestDelegate;

BOOL shouldAnimate = YES, hasAnimated = NO;
int animationDir = 1;
UIAlertView *someError;

- (IBAction) submitLogin:(id) sender {
	if (txtName.text.length > 0 && txtPass.text.length > 0) {
		NSLog(@"Format post request with username");
		[self formatPostRequestWithUsername:txtName.text WithPassword:txtPass.text];
		shouldAnimate = YES;
		animationDir *= -1;
		[txtName resignFirstResponder];
		[txtPass resignFirstResponder];
		[self moveFields:(id) sender];
		
		//test code to move to NavController
		[[[UIApplication sharedApplication] delegate] moveToNav:self];
	} else {
		if (txtName.text.length == 0) {
			[txtName becomeFirstResponder];
			someError = [[UIAlertView alloc] initWithTitle: @"Invalid Input" message: @"Missing Username" delegate: self cancelButtonTitle: @"OK" otherButtonTitles:nil];
			[someError show];
		} else if (txtPass.text.length == 0) {
			[txtPass becomeFirstResponder];
			someError = [[UIAlertView alloc] initWithTitle: @"Invalid Input" message: @"Missing Password" delegate: self cancelButtonTitle: @"OK" otherButtonTitles:nil];
			[someError show];
		}
		
		[someError release];
	}
	
	
	
	/*UIAlertView *someError = [[UIAlertView alloc] initWithTitle: txtName.text message: @"How are you?" delegate: self cancelButtonTitle: @"Dandy" otherButtonTitles:@"Pissed Off",nil];
	 [someError show];
	 [someError release];*/
}

- (IBAction) moveFields:(id) sender {
	
	float ANIM_DURATION = 1.20;
	
	if (shouldAnimate) {
		shouldAnimate = NO;
		lblEnterName.text = @"animated";
		[UIView beginAnimations:@"move_fields" context: nil];
		[UIView setAnimationDelegate: self];
		[UIView setAnimationDuration: ANIM_DURATION];
		[UIView setAnimationCurve: UIViewAnimationCurveEaseInOut];
		contentView.frame = CGRectMake(contentView.frame.origin.x, contentView.frame.origin.y - (animationDir * 220), contentView.frame.size.width, contentView.frame.size.height);
		bgImage.frame = CGRectMake(bgImage.frame.origin.x, bgImage.frame.origin.y - (animationDir * 220), bgImage.frame.size.width, bgImage.frame.size.height);
		[UIView commitAnimations];
	} 
	
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
	if (buttonIndex == 1) {
		UIAlertView *otherError = [[UIAlertView alloc] initWithTitle: txtName.text message: @"ROFL" delegate: self cancelButtonTitle: @">.>" otherButtonTitles: nil];
		[otherError show];
		[otherError release];
	}
}

- (void)formatPostRequestWithUsername:(NSString *)username WithPassword:(NSString *)password {
	requestDelegate = [CommunicationDelegate alloc];
	request = [MNMRequest alloc];
	NSLog(@"Created MNM Request Object");
	[request setURL:@"http://prod.mynovamobile.com/server_app/authenticate_ldap.php" withHTTPMethod:@"POST"];
	NSLog(@"Successfully set url and post");
	NSArray* parameters = [NSArray arrayWithObjects:[MNMRequestParameter withKey:@"username" withValue:username], 
						   [MNMRequestParameter withKey:@"password" withValue:password], 
						   [MNMRequestParameter withKey:@"UDID" withValue:[[UIDevice currentDevice] uniqueIdentifier]],
						   nil];
	[request setPostParameters:parameters];
	
	[request setTimeoutInterval:1.0];
	[[NSURLConnection alloc] initWithRequest:request delegate:requestDelegate];
}

/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/


/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
}
*/


/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}

@end
